package com.wudi.estate.controller.servicemode;

import com.wudi.estate.bean.servicemode.Complain;
import com.wudi.estate.service.assetmanage.DeviceService;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import com.wudi.estate.service.servicemode.ComplainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/complain")
public class ComplainController {
    @Autowired
    private ComplainService complainService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private DeviceService deviceService;

    //投诉列表显示
    @RequestMapping("/listUi")
    public String listUi(ModelMap map) {
        try {
            List<Map<String, Object>> complains = complainService.selectAll();
            map.addAttribute("complains", complains);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "complain_list";
    }
    //    显示添加投诉的页面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        Map<String, Object> maps = null;
        try {
            maps = complainService.selectComplainAndCommunityAndPersonnel();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("maps", maps);
        return "complain_add";
    }
//        添加投诉，返回到“投诉列表显示”里
    @RequestMapping("/add")
    public String add(Complain complain){//使对象自动注入，id和createTime不需要

        try {
            complainService.insert(complain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/complain/addUi";
    }
    //   投诉编辑显示
    @RequestMapping("/editUi")
    public String editUi(ModelMap map, long id){
        System.out.println(id);
        try {
            Map<String,Object> maps= complainService.selectComplainAndPersonnelAndDeviceById(id);
            System.out.println(maps);
            map.addAttribute("maps",maps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "complain_edit";
    }
//    //   投诉编辑逻辑
    @RequestMapping("/edit")
    public String edit(Complain complain){
        System.out.println(complain);
        try {
            complainService.update(complain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/complain/listUi";
    }
//    投诉删除逻辑
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            complainService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(id);
        return "redirect:/complain/listUi";
}
}
